<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from '../mixins/resize'

  const animationDuration = 1000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOption(this.chartData)
      },
      setOption({ xData, yData1,yData2,yData3,yData4,yData5,offset} = {}){
        this.chart.setOption({
          color:['#6bee33','#ff0029','#ed7d31','#409eff','#fff618'],
          title:{
            text:(offset==1?'今日':'最近7天')+'生产，车型趋势图，NG直方图',
            subText:'',
            bottom:0,
            textStyle:{
              lineHeight:25
            },
            left:'center'
          },
          legend: {
            data: ['OK', 'NG','误报','合格率','误报率'],
            top:30
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter: function (params,index) {
              if(params.length>0){
                let str =params[0].name+'<br/>';
                params.forEach((el,index)=>{
                  if(index===3||index===4){
                    str += el.seriesName+' : '+el.value+'%'+'<br/>'
                  }else {
                    str +=  el.seriesName+' : '+el.value+'<br/>';
                  }
                })
                return str;
              }
            },
          },
          grid: {
            top: 100,
            left: '2%',
            right: 50,
            bottom: 50,
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: xData,
              axisTick: {
                alignWithLabel: false
              },
              axisLabel:{
                interval:0,
                rotate:40
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              name: '数量',
              alignTicks:true,
              minInterval:1,
              //splitNumber:10,
              axisTick: {
                show: false
              }
            },
            {
              type: 'value',
              name: '合格率',
              position: 'right',
              alignTicks:true,
              splitNumber:10,
              min:0,
              max:100,
              axisTick: {
                show: false
              },
              splitLine:{
                show:false
              },
              axisLabel: {
                formatter: '{value} %'
              }
            }
          ],
          series: [
            {
              type: 'bar',
              name:'OK',
              stack: 'vistors',
              barWidth: '60%',
              yAxisIndex:0,
              label:{
                show:true,
                position:'inside',
                fontSize:15
              },
              data: yData1,
              animationDuration
            },
            {
              name:'NG',
              type: 'bar',
              stack: 'vistors',
              barWidth: '60%',
              label:{
                show:true,
                position:'inside',
                fontSize:15
              },
              yAxisIndex:0,
              data: yData2,
              animationDuration
            },
            {
              name:'误报',
              type: 'bar',
              stack: 'vistors',
              barWidth: '60%',
              label:{
                show:true,
                position:'inside',
                fontSize:15
              },
              yAxisIndex:0,
              data: yData3,
              animationDuration
            },
            {
              name:'合格率',
              type: 'line',
              barWidth: '60%',
              label:{
                show:true,
                formatter:function (params, ticket, callback){
                  console.log(params)
                  return params.data+'%'
                },
                position:'top',
                fontSize:15
              },
              yAxisIndex:1,
              data: yData4,
              animationDuration
            },
            {
              name:'误报率',
              type: 'line',
              barWidth: '60%',
              label:{
                show:true,
                formatter:function (params, ticket, callback){
                  console.log(params)
                  return params.data+'%'
                },
                position:'top',
                fontSize:15
              },
              yAxisIndex:1,
              data: yData5,
              animationDuration
            }
          ]
        })
        if(yData1.length<=0){
          this.chart.showLoading({
            text:(offset==1?'今日':'最近7天')+'暂无生产数据',
            fontSize: 14,
            showSpinner:false
          })
        }else{
          this.chart.hideLoading()
        }
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.chart.hideLoading()
          this.setOption(val)
        }
      }
    },
  }
</script>
